// pages/topicality/xiaofeiquan/xiaofeiquan.js
//获取应用实例
const app = getApp()
const util = require("../../../utils/util.js");
var QQMapWX = require('../../../utils/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js');
var qqmapsdk = new QQMapWX({
    key: 'KR7BZ-XSQ3P-VGFD7-LNRZL-E7PXE-STF76'
});
const action = "jiujia";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        citycode: "hz",
        city: '杭州',
        province: '浙江',
        pindex: 0,
        cindex: 0,
        cityPick: ["选择城市"],
        newsData: [],
        showtk: false,
        showNav: true, // 快捷导航是否展开
        showShare: true, // 显示分享按钮

        webShow: true, // 网上预约显示
        callShow: false, // 电话预约显示
        onsiteShow: false, // 现场预约显示

        webMore1: false, // 网上预约平台更多显示
        webMore2: false, // 网上预约点位信息更多显示
        callMore: false, //电话预约点位信息更多显示
        onsiteMore: false, // 现场预约点位信息更多显示
        flag: 1,
        subscribeText: "来苗提醒"
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        util.reportMessage("fangwenliang ", { action: "九价疫苗" });
        util.getUrl(app.globalData.url).then((res) => {
            this.setData({
                city: options.city,
                action,
            });
            this.getProvinceData(options.city);
        })
    },

    // 检查是否订阅
    check() {
        util.request("sub_message.php", {
            action: "check",
            name: "九价疫苗",
            city: this.data.city,
            openid: wx.getStorageSync("openid")
        }, "POST").then(res => {
            this.setData({
                subscribeText: res.data.state == 0 ? "来苗提醒" : "已订阅",
            });
        });
    },

    // 订阅成功
    subsuccess() {
        this.setData({
            subscribeText: "已订阅"
        })
    },

    // 获取定位
    getLocation() {
        qqmapsdk.reverseGeocoder({
            success: (res) => {
                let city = res.result.address_component.city;
                // let city="芜湖市";
                let citydata = this.data.citylist.filter(v => {
                    if (city.indexOf(v.cityname) != -1) {
                        return v;
                    }
                });

                if (citydata != '' && city.indexOf(this.data.city) == -1) {
                    wx.showModal({
                        content: '您当前定位城市为' + city + '\n是否需要切换到' + city + '九价疫苗查询？',
                        success: res => {
                            if (res.confirm) {
                                this.setData({
                                    city: Array.isArray(citydata) ? citydata[0].cityname : this.data.city,
                                    citycode: Array.isArray(citydata) ? citydata[0].citycode : this.data.citycode,
                                    province: Array.isArray(citydata) ? citydata[0].province : this.data.province,
                                    cityPick: ["选择城市"]
                                })
                                let clist = [];
                                this.data.provPick.forEach((v, k) => {
                                    if (v == this.data.province) {
                                        this.setData({
                                            pindex: k,
                                        })
                                    }
                                })
                                this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                                    clist.push(v.cityname)
                                })
                                this.setData({
                                    cityPick: this.data.cityPick.concat(clist)
                                })
                                this.data.cityPick.forEach((v, k) => {
                                        if (v == this.data.city) {
                                            this.setData({
                                                cindex: k,
                                            })
                                        }
                                    })
                                    // 获取城市公众号二维码
                                this.getQrcode(this.data.citycode);
                                this.getInfoData(this.data.citycode);
                            }
                        }
                    })
                }

            },
        });
    },

    // 获取城市公众号二维码
    getQrcode(citycode) {
        util.cityConfig(citycode).then(res => {
            this.setData({
                qrcodeimg: res.qrcode,
            })
        })
    },

    // 获取城市
    getProvinceData(optcode) {
        let data = {
            action,
            type: "city",
        };
        util.request('zhuanti.php', data, ).then(res => {
            this.setData({
                cityArray: res.data.data.city,
            })
            let provPick = [];
            let citylist = [];
            let clist = [];
            this.data.cityArray.forEach(v => {
                v.citylist.forEach(v => {
                    citylist.push(v)
                })
                provPick.push(v.province)
                this.setData({
                    provPick,
                    citylist
                })
            })
            if (this.data.citylist) {
                this.data.citylist.forEach((v, k) => {
                    if (optcode == v.citycode) {
                        this.setData({
                            citycode: v.citycode,
                            city: v.cityname,
                            province: v.province,
                        })
                    } else {
                        this.setData({
                            citycode: this.data.citycode,
                            city: this.data.city,
                            province: this.data.province,
                        })
                    }
                })
            }

            this.data.provPick.forEach((v, k) => {
                if (v == this.data.province) {
                    this.setData({
                        pindex: k,
                    })
                }
            })

            this.data.cityArray[this.data.pindex].citylist.forEach(v => {
                clist.push(v.cityname)
            })
            this.setData({
                cityPick: this.data.cityPick.concat(clist)
            })

            this.data.cityPick.forEach((v, k) => {
                    if (v == this.data.city) {
                        this.setData({
                            cindex: k,
                        })
                    }
                })
                // 获取城市公众号二维码
            this.getQrcode(this.data.citycode);
            this.getInfoData();
            if (this.data.citylist != "" && Array.isArray(this.data.citylist)) {
                // 获取定位
                this.getLocation();
            }

        })
    },

    getInfoData(citycode) {
        let data = {
            action,
            // type: "city",
            citycode: citycode ? citycode : this.data.citycode
        };

        this.check();
        util.request('zhuanti.php', data, ).then(res => {
            if (res.data.data.daofen) {
                res.data.data.daofen.content = res.data.data.daofen.content
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(style="[^"]+")))/ig, '<p')
                    .replace(/<p([\s\w"=\/\.:;]+)((?:(class="[^"]+")))/ig, '<p')
                    .replace(/<p>/ig, '<p class="p_class">')
                    .replace(/<img([\s\w"-=\/\.:;]+)((?:(style="[^"]+")))/ig, '<img$1')
                    .replace(/<img/gi, '<img style="width:100%;height:auto"')
                    // .replace(/<span>/ig, '<p class="p_class">')
            }
            wx.setNavigationBarTitle({
                title: res.data.data ? res.data.data.title : "九价疫苗查询"
            })
            let newsData = res.data.data.xiaoxi;
            let callData = res.data.data.calldata;
            let onsiteDate = res.data.data.onsitedate;
            let website = res.data.data.website;

            if (onsiteDate.place)
                onsiteDate.place.forEach(item => {
                    item.yy_time = new Date(item.yy_time).getTime() ? item.yy_time : "";
                });

            if (callData.place)
                callData.place.forEach(item => {
                    item.yy_time = new Date(item.yy_time).getTime() ? item.yy_time : "";
                });

            if (website.place)
                website.place.forEach(item => {
                    item.yy_time = new Date(item.yy_time).getTime() ? item.yy_time : "";
                });

            this.setData({
                infoData: res.data.data,
                daofen: res.data.data.daofen,
                title: res.data.data.title,
                newsData,
                website,
                callData,
                onsiteDate,
                flag: website ? '1' : callData ? '2' : '3'
            });



            if (this.data.website.platform && this.data.website.platform.length >= 2) {
                this.setData({
                    webMore1: true
                })
            }
            if (this.data.website.place && this.data.website.place.length >= 1) {
                this.setData({
                    webMore2: true
                })
            }

            if (this.data.callData.place.length >= 2) {
                this.setData({
                    callMore: true
                })
            }

            if (this.data.onsiteDate.place.length >= 2) {
                this.setData({
                    onsiteMore: true
                })
            }
        });
    },

    // 省份选择
    bindProv: function(e) {
        this.setData({
            pindex: e.detail.value,
            cindex: 0,
            cityPick: ["选择城市"],
        })
        let clist = [];
        this.data.cityArray[this.data.pindex].citylist.forEach(v => {
            clist.push(v.cityname)
        })
        this.setData({
            cityPick: this.data.cityPick.concat(clist),
        })
    },

    // 城市选择
    bindCity(e) {
        this.setData({
            cindex: e.detail.value
        })
        let cityname = this.data.cityPick[this.data.cindex];
        if (this.data.citylist) {
            this.data.citylist.forEach(v => {
                if (v.cityname == cityname) {
                    let citycode = v.citycode;
                    this.setData({
                        citycode,
                        city: cityname
                    });
                }
            })
        }
        // 获取城市公众号二维码
        this.getQrcode(this.data.citycode);
        this.getInfoData(this.data.citycode);
    },

    // 标题点击事件
    btnClick(e) {
        let flag = e.currentTarget.dataset.flag;
        this.setData({
            flag,
        });
        if (flag == 1) {
            this.setData({
                webShow: true,
                callShow: false,
                onsiteShow: false,
            })
        } else if (flag == 2) {
            this.setData({
                webShow: false,
                callShow: true,
                onsiteShow: false,
            })
        } else if (flag == 3) {
            this.setData({
                webShow: false,
                callShow: false,
                onsiteShow: true,
            })
        }
    },

    // 查看详情点击事件
    seeDetail(e) {
        let url = util.getValue("url", e);
        let title = util.getValue("title", e);
        if (url.includes("http")) {
            this.setData({
                showtk: true
            })
            util.navContact(title, url)
        }

    },

    // 查看更多
    seemoreBtn(e) {
        let flag = e.currentTarget.dataset.flag;
        if (flag == 1) {
            this.setData({
                webMore1: false,
            })
        } else if (flag == 2) {
            this.setData({
                webMore2: false,
            })
        } else if (flag == 3) {
            this.setData({
                callMore: false,
            })
        } else if (flag == 4) {
            this.setData({
                onsiteMore: false,
            })
        }
    },

    // 最新消息点击事件
    newsBtn(e) {
        let index = util.getValue("index", e);
        let data = this.data.newsData[index];
        if (data && data.url && data.url_arr) {
            let id = data.url_arr.id;
            let title = data.title;
            let url = data.url_arr.url;
            let type = data.url_arr.type;
            let citycode = data.citycode;
            if (id && id != 0) {
                wx.navigateTo({
                    url: '/pages/article/detail/detail?id=' + id + '&type=' + type + '&city=' + citycode,
                })
            } else if (url.includes("mp.weixin.qq.com")) {
                wx.navigateTo({
                    url: `/pages/webview/webview?path=${url}`,
                });
            } else {
                this.setData({
                    showtk: true
                })
                util.navContact(title, url)
            }
        }

    },

    // 地图点击事件
    mapBtn(e) {
        let lng = e.currentTarget.dataset.lng;
        let lat = e.currentTarget.dataset.lat;
        let name = e.currentTarget.dataset.name;
        let tel = e.currentTarget.dataset.tel;
        let addr = e.currentTarget.dataset.addr;
        let flag = e.currentTarget.dataset.flag;
        wx.navigateTo({
            url: '/pages/map/map?lng=' + lng + '&lat=' + lat + '&name=' + name + '&tel=' + tel + '&addr=' + addr + '&flag=' + flag,
        })
    },


    // 进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
    },

    // 关闭
    close() {
        this.setData({
            showtk: false,
        })
    },

    /**
     * 页面滑动事件的处理函数
     */
    onPageScroll: function(e) {
        this.setData({
            showNav: false,
        })
    },

    control(e) {
        let index = util.getValue("index", e);
        let type = util.getValue("type", e);
        let navitype = util.getValue("navitype", e);
        let data = this.data[type].place[index];
        if (navitype == "path" && data.course && data.course_arr) {
            if (data.course.includes("mp.weixin.qq.com"))
                wx.navigateTo({
                    url: `/pages/webview/webview?path=${data.url}`,
                });
            else
                wx.navigateTo({
                    url: `/pages/article/detail/detail?id=${data.course_arr.id}&type=${data.course_arr.type}&city=${data.citycode}`,
                });
        } else if (navitype == "method") {
            if (data.url) {
                this.setData({
                    showtk: true
                })
                util.navContact(data.name, data.url)
            }
        }
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {
        return {
            title: this.data.title ? this.data.title : "九价疫苗查询",

        }
    },

    // 分享朋友圈
    onShareTimeline: function() {
        return {
            title: this.data.title ? this.data.title : "九价疫苗查询",
            imageUrl: "http://imgbdb3.bendibao.com/xcx/20206/23/2020623094828_71858.jpg",
            query: { city: this.data.citycode },
        }
    },
})